<?php

namespace app\common\resourceHandle;

use app\common\library\Helper\DateTime;
use app\common\library\TencentMap;
use app\common\model\Order;
use app\common\model\OrderGoods;
use think\Model;

class ShopResource extends ResourceHandle
{

    /**
     * @param Model $model
     * @return array
     * @throws \think\Exception
     */
    public function handle(Model $model): array
    {
        $model->hidden([
            'money', 'agent_id', 'logintime', 'loginip',
            'fixed_amount', 'business_license', 'createtime',
            'updatetime', 'deletetime', 'source'
        ]);
        $res = $model->toArray();
        $this->dataUp(['goods_name'], $res['shop_goods'], 'goods');
        if(!empty($res['shop_goods'])) {
            foreach ($res['shop_goods'] as &$r) {
                $r['sale_num'] = OrderGoods::getSaleNumByShopGoodsId($r['shop_id'], $r['goods_id']);
            }
        }
        $res['business_week'] = DateTime::chineseWeek($res['business_week']);
        unset($res['logintime_text']);
        unset($res['createtime_text']);
        unset($res['shop_id']);
        $res['sale_num'] = $res['sale_num'] ?? 0;
        return $res;
    }

    /**
     * 商家详情处理
     * @param Model $model
     * @return array
     * @throws \think\Exception
     */
    public function info(Model $model): array
    {
        $model->shopImages;
        return $this->handle($model);
    }

    /**
     * 商家端-商家信息
     * @param Model $model
     * @return array
     * @throws \think\Exception
     */
    public function shopInfo(Model $model): array
    {
        $model->visible(['id', 'logo', 'shop_name', 'phone', 'address', 'score', 'business_start_time', 'business_end_time', 'business_week', 'money','longitude','latitude']);
        return $model->toArray();
    }
}